package com.hzwl.core

import com.hzwl.core.Flag.flag
import kotlinx.serialization.Serializable
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.io.File


/**
 * TODO
 * @author lin
 */


@Serializable
data class ZuvData(
    val z2: MutableList<Double> = mutableListOf(),
    val w2: MutableList<Double> = mutableListOf(),
    val fi: MutableList<Double> = mutableListOf(),
    val h2: MutableList<Double> = mutableListOf(),
    val u2: MutableList<Double> = mutableListOf(),
    val v2: MutableList<Double> = mutableListOf(),
    var color: MutableList<String> = mutableListOf()
) {

    val maxH2: Double
        get() = h2.maxOrNull() ?: 0.0

    val minH2: Double
        get() = h2.minOrNull() ?: 0.0


    fun setColor() {
        val list = mutableListOf<String>()
        val intervals = calculateIntervals(15)
        h2.forEach { value ->
            when(value) {
                in 1.0..2.0 -> list.add(color1)
                in 2.0..3.0 -> list.add(color2)
                in 3.0..4.0 -> list.add(color3)
                in 4.0..5.0 -> list.add(color4)
                in 5.0..6.0 -> list.add(color5)
                in 6.0..7.0 -> list.add(color6)
                in 7.0..8.0 -> list.add(color7)
                in 8.0..9.0 -> list.add(color8)
                in 9.0..10.0 -> list.add(color9)
                in 10.0..11.0 -> list.add(color10)
                in 11.0..12.0 -> list.add(color11)
                in 12.0..13.0 -> list.add(color12)
                in 13.0..14.0 -> list.add(color13)
                in 14.0..15.0 -> list.add(color14)
                in 15.0..16.0 -> list.add(color15)
            }
        }
        color = list
    }

    // 根据最大值和最小值计算区间
    private fun calculateIntervals(numIntervals: Int): List<Double> {
        val intervalSize = (maxH2 - minH2) / numIntervals
        return (0 until numIntervals).map { minH2 + it * intervalSize }
    }

    // 根据值获取对应的颜色
    private fun getColorForValue(value: Double, intervals: List<Double>): String {
        for (i in 1 until intervals.size) {
            if (value >= intervals[i - 1] && value < intervals[i]) {
                // 根据区间返回对应的颜色
                return when (i) {
                    1 -> color1
                    2 -> color2
                    3 -> color3
                    4 -> color4
                    5 -> color5
                    6 -> color6
                    7 -> color7
                    8 -> color8
                    9 -> color9
                    10 -> color10
                    11 -> color11
                    12 -> color12
                    13 -> color13
                    14 -> color14
                    else -> color15
                }
            }
        }
        return "" // 如果未找到对应区间，返回空字符串或其他默认值
    }


    companion object {
        val colorList = arrayListOf("#2468B4", "#3C9DC2", "#80CDC1", "#97DAA8", "#C6E7B5", "#EEF7D9", "#FFEE9F", "#FCD97D", "#FFB664", "#FC964B", "#FA7034", "#F54020", "#ED2D1C", "#DC1820", "#B40023")
        val color1 = "#2468B4"
        val color2 = "#3C9DC2"
        val color3 = "#80CDC1"
        val color4 = "#97DAA8"
        val color5 = "#C6E7B5"
        val color6 = "#EEF7D9"
        val color7 = "#FFEE9F"
        val color8 = "#FCD97D"
        val color9 = "#FFB664"
        val color10 = "#FC964B"
        val color11 = "#FA7034"
        val color12 = "#F54020"
        val color13 = "#ED2D1C"
        val color14 = "#DC1820"
        val color15 = "#B40023"
    }
}

var h2StartReading = false
var u2StartReading = false
var v2StartReading = false
var z2StartReading = false
var w2StartReading = false
var fiStartReading = false

fun resetFlags(
    z2: Boolean = false,
    w2: Boolean = false,
    fi: Boolean = false,
    h2: Boolean = false,
    u2: Boolean = false,
    v2: Boolean = false
) {
    z2StartReading = z2
    w2StartReading = w2
    fiStartReading = fi
    h2StartReading = h2
    u2StartReading = u2
    v2StartReading = v2
}

fun readValues(line: String, list: MutableList<Double>) {
    val values = line.trim().split("\\s+".toRegex())
    values.forEach { value ->
        if (value.isNotEmpty()) {
            list.add(value.toDouble())
        }
    }
}

@Service
class ExeHandle(
    val runService: RunService
) {

    @Value("\${out.path}")
    lateinit var outPath: String

    fun handle(speed: String, hours: Int): Map<String, Any> {
        if (File("$outPath/$speed---0.geojson").exists()) {
//        if (minioService.exist("$speed#1.geojson")) {
            return mapOf("code" to 2, "msg" to "获取成功", "data" to List(hours) { i ->
                "http://218.59.186.166:10096/hydrodynamics/file/$speed---$i.geojson"
            })
        }
        if (flag) return mapOf("code" to 1, "msg" to "程序正在运行中")
        else {

            flag = true
            runService.generate(speed)
            return mapOf("code" to 0, "msg" to "程序运行成功")
        }
    }
}


fun main() {

    val filename = "/Users/linzili/Downloads/INITIALU1.DAT"
    val lines = mutableListOf<String>()
    lines.add("u1")
    repeat(3808) {
        lines.add("0.0")
    }
    File(filename).writeText(lines.joinToString("\n") + "\n")
}
